import 'package:fameex_app/common/common.dart';
import 'package:event_bus/event_bus.dart';
import 'package:fameex_app/events/home/home_market_event.dart';
import 'package:fameex_app/model/socket/socket_home_market.dart';

class Page1 extends StatefulWidget {
  @override
  _Page1State createState() => _Page1State();
}

class _Page1State extends State<Page1> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    //全局事件通知
    EventBus eventBus = EventBus();

    TimerUtil _timer = TimerUtil(mInterval: 1000, mTotalTime: 60000);

    // 模拟长连接
    _timer.setOnTimerTickCallback((int time) {
      // 发事件
      eventBus.fire(HomeMarketEvent()
        ..model = SocketHomeMarketModel.fromJson({'list': []}));

      print("TimerTick: " + time.toString());
    });

    _timer.startCountDown();

    //
    StreamSubscription listen = eventBus.on<HomeMarketEvent>().listen((e) {
      print("监听事件执行了啊1111111111111");
      print(e.model.list);
    });
    //  listen?.cancel();  //取消监听回调函数

    // 监听事件
    StreamSubscription listen2 = eventBus.on<HomeMarketEvent>().listen((e) {
      print("监听事件执行了啊2222222222222");
      print(e.model.list);
    });
    //  listen2?.cancel();  //取消监听回调函数
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text('我是page1',
                style: TextStyle(
                  fontSize: sp(16),
                  decoration: TextDecoration.none,
                  fontWeight: FontWeight.w500,
                  color: LightColors.color_666666,
                )),
            SizedBox(height: wh(30)),
          ],
        ),
      ),
    );
  }
}
